<?php

if (!isset($_REQUEST['expense']) || !is_numeric($_REQUEST['expense'])) {
    error_obj::setError(new error_obj("bad request expense", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
    base_obj::redirect(ROOT . '?input=fixed_expense');
}

$user_obj = new user_obj();
$expense_obj = new expense_obj();

$expenseId = (int) $_REQUEST['expense'];

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'edit_expense':
            $expense = $_REQUEST['expense_data'];
            $expense['from'] = base_obj::convertDateTo($expense['from']);
            $result = $expense_obj->editExpense($expense, $expenseId);
            if (error_obj::isError($result)) {
                error_obj::setError($result, UNEXCEPTED);
            } else {
                base_obj::redirect(ROOT . '?input=fixed_expense');
            }

            break;
    }
}

$expense_data = $expense_obj->getExpenseById($expenseId);

if (error_obj::isError($expense_data)) {
    error_obj::setError($expense_data, UNEXCEPTED);
} elseif (empty($expense_data)) {
    error_obj::setError(new error_obj("empty expense", __FILE__, 'get expense data'), 'Empty expense');
} else {
    $filter = array(
        'levels' => array(
            USER_TYPE_ADMIN, USER_TYPE_TECHNIC
        )
    );
    $GLOBALS['template_data']['users'] = $user_obj->getUsers("*", $filter, $GLOBALS['list_of_users_order']);
    $expense_data['from'] = base_obj::convertDateTo($expense_data['from'], 'dd/mm/yyyy', 'date');
    $GLOBALS['template_data']['expense_data'] = $expense_data;
}
?>
